package com.xomily.devops.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.xomily.common.annotation.Log;
import com.xomily.common.core.domain.AjaxResult;
import com.xomily.common.enums.BusinessType;
import com.xomily.common.utils.ServletUtils;
import com.xomily.common.utils.ip.IpUtils;
import com.xomily.devops.domain.DevAlarmLog;
import com.xomily.devops.domain.DevCpuLog;
import com.xomily.devops.domain.DevDisc;
import com.xomily.devops.domain.DevDiscLog;
import com.xomily.devops.domain.DevHost;
import com.xomily.devops.domain.DevMemLog;
import com.xomily.devops.domain.DevNet;
import com.xomily.devops.domain.DevNetLog;
import com.xomily.devops.service.IDevAlarmLogService;
import com.xomily.devops.service.IDevCpuLogService;
import com.xomily.devops.service.IDevDiscLogService;
import com.xomily.devops.service.IDevDiscService;
import com.xomily.devops.service.IDevHostService;
import com.xomily.devops.service.IDevMemLogService;
import com.xomily.devops.service.IDevNetLogService;
import com.xomily.devops.service.IDevNetService;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 接收数据
 * 
 * @author xomily
 */
@RestController
public class DevAgentController {
	/**
	 * 主机任务错误日志
	 */
	@Autowired
	private IDevCpuLogService devCpuLogService;
	@Autowired
	private IDevMemLogService devMemLogService;
	@Autowired
	private IDevNetLogService devNetLogService;
	@Autowired
	private IDevDiscLogService devDiscLogService;
	@Autowired
	private IDevAlarmLogService devAlarmLogService;
	@Autowired
	private IDevHostService devHostService;
	@Autowired
	private IDevNetService devNetService;
	@Autowired
	private IDevDiscService devDiscService;
	
	@SuppressWarnings("finally")
	@PostMapping("/devops/agent/info")
	public AjaxResult infoTask(@RequestBody String param) {
		JSONObject infoJsonObject = (JSONObject) JSONUtil.parse(param);
		JSONObject hostInfoJsonObject = infoJsonObject.getJSONObject("host");
		JSONArray netInfoJsonObject = infoJsonObject.getJSONArray("netList");
		JSONArray discInfoJsonObject = infoJsonObject.getJSONArray("discList");
		JSONObject alarmJsonObject = infoJsonObject.getJSONObject("alarmLog");
		String resultString ="success";
		
		String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
		DevHost hostQuery = devHostService.selectDevHostByIp(ip);
		if (hostQuery == null) {
			return AjaxResult.error("reject");
		} else {
			if (hostQuery.getStatus()=="0"){
				return AjaxResult.error("suspend");	
			}
		}
		try {

			if (hostInfoJsonObject != null) {
				DevHost devHost = new DevHost();
				BeanUtil.copyProperties(hostInfoJsonObject, devHost);
				devHost.setIp(ip);
				devHostService.updateAgentHost(devHost);
			}


			if (netInfoJsonObject != null) {
				List<DevNet> devNetList = JSONUtil.toList(netInfoJsonObject, DevNet.class);
				for (DevNet devNet : devNetList) {
					devNet.setIp(ip);
					devNetService.insertDevNet(devNet);
				}
			}

			if (discInfoJsonObject != null) {
				List<DevDisc> devDiscList = JSONUtil.toList(discInfoJsonObject, DevDisc.class);
				for (DevDisc devDisc : devDiscList) {
					devDisc.setIp(ip);
					devDiscService.insertDevDisc(devDisc);
				}
			}

			if (alarmJsonObject != null) {
				DevAlarmLog devAlarmLog = new DevAlarmLog();
				BeanUtil.copyProperties(alarmJsonObject, devAlarmLog);
				devAlarmLog.setIp(ip);
				devAlarmLogService.insertDevAlarmLog(devAlarmLog);
			}

			
		} catch (Exception e) {
			e.printStackTrace();
			resultString = "error：" + e.toString();
		} finally {
			return AjaxResult.success(resultString);
		}

	}
	@SuppressWarnings("finally")
	@PostMapping("/devops/agent/survey")
	public AjaxResult surveyTask(@RequestBody String param) {
		JSONObject surveyJsonObject = (JSONObject) JSONUtil.parse(param);
		JSONObject cpuJsonObject = surveyJsonObject.getJSONObject("cpuLog");
		JSONObject memJsonObject = surveyJsonObject.getJSONObject("memLog");
		JSONArray netJsonObject = surveyJsonObject.getJSONArray("netLogList");
		JSONArray discJsonObject = surveyJsonObject.getJSONArray("discLogList");
		JSONObject alarmJsonObject = surveyJsonObject.getJSONObject("alarmLog");
		String resultString ="success";
		
		String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
		DevHost hostQuery = devHostService.selectDevHostByIp(ip);
		if (hostQuery == null) {
			return AjaxResult.error("reject");
		} else {
			if (hostQuery.getStatus()=="0"){
				return AjaxResult.error("suspend");	
			}
		}
		try {

			if (cpuJsonObject != null) {
				DevCpuLog devCpuLog = new DevCpuLog();
				BeanUtil.copyProperties(cpuJsonObject, devCpuLog);
				devCpuLog.setIp(ip);
				devCpuLogService.insertDevCpuLog(devCpuLog);
			}
			
			if (netJsonObject != null) {
				List<DevNetLog> devNetLogList = JSONUtil.toList(netJsonObject, DevNetLog.class);
				for (DevNetLog devNetLog : devNetLogList) {
					devNetLog.setIp(ip);
					devNetLogService.insertDevNetLog(devNetLog);
				}
			}
			
			if (discJsonObject != null) {
				List<DevDiscLog> devDiscLogList = JSONUtil.toList(discJsonObject, DevDiscLog.class);
				for (DevDiscLog devDiscLog : devDiscLogList) {
					devDiscLog.setIp(ip);
					devDiscLogService.insertDevDiscLogByAgent(devDiscLog);
				}
			}

			if (memJsonObject != null) {
				DevMemLog devMemLog = new DevMemLog();
				BeanUtil.copyProperties(memJsonObject, devMemLog);
				devMemLog.setIp(ip);
				devMemLogService.insertDevMemLog(devMemLog);
			}



			if (alarmJsonObject != null) {
				DevAlarmLog devAlarmLog = new DevAlarmLog();
				BeanUtil.copyProperties(alarmJsonObject, devAlarmLog);
				devAlarmLog.setIp(ip);
				devAlarmLogService.insertDevAlarmLog(devAlarmLog);
			}

			
		} catch (Exception e) {
			e.printStackTrace();
			resultString = "error：" + e.toString();
		} finally {
			return AjaxResult.success(resultString);
		}

	}
}
